#include "NetworkThread.h"
#include <iostream>

using namespace std;

void error(const char *msg)
{
    perror(msg);
    exit(0);
}

void NetworkThread::run()
{

    //EZ MUXIK!
    int sockfd, portno, n;
    struct sockaddr_in serv_addr;
    struct hostent *server;

    float *buffer = new float;

    portno = 1337;
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0)
        error("ERROR opening socket");
    server = gethostbyname("127.0.0.1");
    if (server == NULL) {
        fprintf(stderr,"ERROR, no such host\n");
        exit(0);
    }
    bzero((char *) &serv_addr, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    bcopy((char *)server->h_addr,
         (char *)&serv_addr.sin_addr.s_addr,
         server->h_length);
    serv_addr.sin_port = htons(portno);
    if (::connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
        error("ERROR connecting");

    while(1)
    {
        n = read(sockfd,buffer,4);
        emit modsign(*buffer);
        if (n < 0)
            error("ERROR reading from socket");
        printf("%f\n", *buffer);
    }
    ::close(sockfd);
}

